//
//  DSZLoginViewController.m
//  7.13苏宁框架
//
//  Created by app05 on 15/7/19.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "DSZLoginViewController.h"
#import "DSZNavigationBarView.h"
#import "DSZMoreView.h"
#import "DSZMineLoginView.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

#import "DSZMainViewController.h"
#import "HomeViewController.h"
#import "DSZShopViewController.h"
@interface DSZLoginViewController ()<navigationBarDelegata,tableViewIndexPathRowDelegate,DSZMineLoginViewDelegate>

@property(nonatomic,assign)BOOL flag;
@property(nonatomic,weak)UIView *moreView;
@property(nonatomic,strong)DSZMineLoginView *loginview;
@end

@implementation DSZLoginViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *view=[[UIView alloc]initWithFrame:self.view.bounds];
    view.backgroundColor=[UIColor whiteColor];
    self.view=view;
    
    [self addNavBar];
    
    [self addContentView];
    
    // 监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:_loginview.accountField];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:_loginview.pwdField];
    
    // 读取上次的配置
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _loginview.accountField.text = [defaults objectForKey:@"zh2"];
    // 处理密码
    _loginview.pwdField.text = [defaults objectForKey:@"mm"];


    
}

- (void)textChange
{
    // 控制按钮的状态
    
    _loginview.clickLogin.enabled = (_loginview.accountField.text.length && _loginview.pwdField.text.length);
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark---代理
-(void)clickLoginBtnDelegate
{
    if (![_loginview.accountField.text isEqualToString:@"disizu"]) {
        // 帐号不存在
        [MBProgressHUD showError:@"帐号不存在"];
        return;
    }
    
    if (![_loginview.pwdField.text isEqualToString:@"666666"]) {
        // 密码错误
        [MBProgressHUD showError:@"密码错误"];
        return;
    }
    
    
    
    
    
    // 在一秒一直调用括号里面的内容
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        
        [self.navigationController popViewControllerAnimated:YES];
        // 存储数据
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        [defaults setObject:_loginview.accountField.text forKey:@"zh2"];
        [defaults setObject:_loginview.pwdField.text forKey:@"mm"];
        
        [defaults synchronize];
        
        
        //发通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
        
    });

}

-(void)addNavBar
{
    DSZNavigationBarView *nav=[[DSZNavigationBarView alloc]initWithFrame:CGRectZero];
    nav.frame=CGRectMake(0, 20, 320, 44);
    [self.view addSubview:nav];
    nav.delegate=self;
    nav.titleLabel=@"登录";
}
-(void)addContentView
{
    _loginview=[DSZMineLoginView mineLoginView];
    [self.view bringSubviewToFront:_loginview];
    _loginview.delegate=self;
    _loginview.frame=CGRectMake(0, 63, 320, 416);
    [self.view addSubview:_loginview];
}
-(void)btnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)moreBtnClick
{
    if (!self.flag) {
        DSZMoreView *more=[DSZMoreView moreView];
        more.delegate=self;
        more.frame=CGRectMake(185, 65, 146, 186);
        self.moreView=more;
        [self.view addSubview:self.moreView];
        self.flag=YES;
    }
    else
    {
        [self.moreView removeFromSuperview];
        self.moreView=nil;
        self.flag=NO;
    }
}
-(void)dszMoreViewWithIndexRow:(NSInteger)indexPathRow
{
    if (indexPathRow==0) {
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        DSZMainViewController *main=[story instantiateViewControllerWithIdentifier:@"main"];
        //DSZMainViewController *main=[[DSZMainViewController alloc]init];
        [self presentViewController: main animated:NO completion:nil];
    }else if (indexPathRow==1)
    {}
    else if (indexPathRow==2)
    {
        UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
        
        DSZShopViewController *shop=[story instantiateViewControllerWithIdentifier:@"shop"];
        [self.navigationController pushViewController:shop animated:NO];
        self.flag=YES;
        [self moreBtnClick];
        DSZMainViewController *main=(DSZMainViewController*)self.tabBarController;
        [main hiddenMainTabbar:YES];
    }else if (indexPathRow==3)
    {
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
    
}
@end
